package org.example.taxi.service;

import org.example.taxi.repository.HiveRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class HiveService {
    private static Map<String, String> vendorMap = null;
    private static Map<String, String> payMap = null;

    static {
        vendorMap = new HashMap<>();
        vendorMap.put("1", "Creative Mobile Technologies");
        vendorMap.put("2", "VeriFone Inc.");

        payMap = new HashMap<>();
        payMap.put("1", "信用卡");
        payMap.put("2", "现金");
        payMap.put("3", "不收费");
    }

    @Autowired
    private HiveRepository hiveRepository;

    /**
     * 执行查询
     *
     * @param sql SQL 语句
     * @return 查询结果
     */
    public List<Map<String, Object>> executeQuery(String sql) {
        return this.hiveRepository.query(sql);
    }

    public Map<String, List<Object>> chart2() {
        // [{"x":"00","y":13166},{"x":"01","y":15095},{"x":"02","y":12905},{"x":"03","y":10229},{"x":"04","y":6678},{"x":"05","y":3271},{"x":"06","y":2595},{"x":"07","y":2832},{"x":"08","y":2774},{"x":"09","y":3758},{"x":"10","y":5091},{"x":"11","y":7018},{"x":"12","y":8331},{"x":"13","y":3660},{"x":"14","y":34},{"x":"15","y":8},{"x":"16","y":25},{"x":"17","y":22},{"x":"18","y":16},{"x":"19","y":32},{"x":"20","y":28},{"x":"21","y":28},{"x":"22","y":14},{"x":"23","y":105}]
        return getXY(this.hiveRepository.chart2());
    }

    public List<Map<String, Object>> chart3() {
        return this.hiveRepository.chart3();
    }

    public List<Map<String, Object>> chart4() {
        return this.hiveRepository.chart4();
    }

    public List<Map<String, Object>> chart5() {
        List<Map<String, Object>> resultMap = this.hiveRepository.chart5();
        for (Map<String, Object> map : resultMap) {
            String val = payMap.getOrDefault(map.get("name").toString(), "无");
            map.put("name", val);
        }
        return resultMap;
    }

    public Map<String, List<Object>> chart6() {
        return getXY(this.hiveRepository.chart6());
    }

    public Map<String, List<Object>> chart7() {
        return getXY(this.hiveRepository.chart7());
    }

    public List<Map<String, Object>> chart8() {
        List<Map<String, Object>> resultMap = this.hiveRepository.chart8();
        for (Map<String, Object> map : resultMap) {
            String val = vendorMap.getOrDefault(map.get("name").toString(), "未知");
            map.put("name", val);
        }
        return resultMap;
    }

    public Map<String, List<Object>> chart9() {
        return getXY(this.hiveRepository.chart9());
    }

    public Map<String, List<Object>> chart10() {
        List<Map<String, Object>> resultMap = this.hiveRepository.chart10();
        for (Map<String, Object> map : resultMap) {
            String val = vendorMap.getOrDefault(map.get("x").toString().replace("供应商", ""), "未知");
            map.put("x", val);
        }
        return getXY(resultMap);
    }

    public Map<String, List<Object>> chart11() {
        Map<String, List<Object>> result = new HashMap<>();
        result.put("x_data", new ArrayList<>());
        result.put("y_data1", new ArrayList<>());
        result.put("y_data2", new ArrayList<>());
        for (Map<String, Object> map : this.hiveRepository.chart11()) {
            String paymentType = payMap.getOrDefault(map.get("payment_type").toString(), "无");
            Double total = Double.parseDouble(map.get("total").toString());
            Double tip_pct = Double.parseDouble(map.get("tip_pct").toString());
            double rest = total - tip_pct;
            List<Object> xData = result.get("x_data");
            xData.add(paymentType);

            List<Object> yData1 = result.get("y_data1");
            yData1.add(Math.round(rest * 100.0) / 100.0);

            List<Object> yData2 = result.get("y_data2");
            yData2.add(tip_pct);
        }
        return result;
    }

    public Map<String, Map<String, List<Object>>> chart12() {
        // 1. 按 vendor_id 分组
        List<Map<String, Object>> maps = this.hiveRepository.chart12();
        List<String> months = maps.stream().map(x -> x.get("dt").toString()).distinct().sorted(Comparator.comparing(x -> x)).collect(Collectors.toList());
        // 提取 x轴
        List<Object> xAxis = new ArrayList<>(months);
        Map<String, List<Map<String, Object>>> vendorDataMap = maps.stream()
                .collect(Collectors.groupingBy(row -> row.get("vendor_id").toString()));

        // 2. 对每个供应商的数据进行处理
        Map<String, Map<String, List<Object>>> chartDataMap = new HashMap<>();
        for (Map.Entry<String, List<Map<String, Object>>> entry : vendorDataMap.entrySet()) {
            String vendorId = entry.getKey();
            List<Map<String, Object>> vendorData = entry.getValue();

            // 按月份分组，存储价格
            Map<String, Object> monthToPriceMap = new LinkedHashMap<>();
            for (Map<String, Object> row : vendorData) {
                String month = row.get("dt").toString();
                Object price = ((Number) row.get("avg_total_price")).doubleValue();
                monthToPriceMap.put(month, price);
            }
            // 提取 y轴
            List<Object> yAxis = new ArrayList<>();
            for (String month : months) {
                yAxis.add(monthToPriceMap.getOrDefault(month, 0));
            }

            // 存储该供应商的数据
            Map<String, List<Object>> chartData = new HashMap<>();
            chartData.put("xAxis", xAxis);
            chartData.put("yAxis", yAxis);
            chartDataMap.put(vendorId, chartData);
        }

        return chartDataMap;
    }

    public static Map<String, List<Object>> getXY(List<Map<String, Object>> list) {
        Map<String, List<Object>> result = new HashMap<>();
        result.put("x_data", new ArrayList<>());
        result.put("y_data", new ArrayList<>());
        for (Map<String, Object> map : list) {
            Object x = map.get("x");
            Object y = map.get("y");

            List<Object> xList = result.get("x_data");
            xList.add(x);
            result.put("x_data", xList);

            List<Object> yList = result.get("y_data");
            yList.add(y);
            result.put("y_data", yList);
        }
        return result;
    }


}